using System.Collections;
using System.Drawing;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Input;

namespace NielsRask.Mucus
{
	public abstract class Control
	{
		protected Control parentControl;
		public abstract void Render( Surface screen );
		protected Size size = new Size( 65, 25 );
		protected Point position = new Point( 0, 0 );
		//protected Point offset = new Point(0,0);
		protected bool enabled = true;
		protected bool visible = true;
		// enabled og visible

        public bool DrawBorder { get; set; }

		public event SdlButtonHandler OnControlClicked;
		public delegate void SdlButtonHandler(Control control);

		public event SdlControlCollectionEvent OnSizeChanged;
		//public event SdlControlCollectionEvent OnItemRemoved;
		public delegate void SdlControlCollectionEvent( Control control );

		public Control()
		{
			//Events.MouseButtonUp += Events_MouseButtonUp;
            DrawBorder = true;
		}

		void Events_MouseButtonUp( object sender, MouseButtonEventArgs e )
		{
			if (OnControlClicked != null)
			{
				bool wasClicked = IsPointWithinControl(e.Position);
				if ( wasClicked )
					OnControlClicked(this);
			}
		}

        public abstract bool Click(MouseButtonEventArgs e);

		public Control ParentControl
		{
			get { return parentControl; }
			set { parentControl = value; }
		}
		public Point Position
		{
			get { return position; }
			set { position = value; }
		}
		public Size Size
		{
			get { return size; }
			set
			{
				size = value;
				if ( OnSizeChanged != null )
					OnSizeChanged(this);
			}
		}

		public int Height
		{
			get { return size.Height;  }
			set { size.Height = value;}
		}
		public int Width
		{
			get { return size.Width;  }
			set { size.Width = value;}
		}

		public Point Offset
		{
			get
			{
				if ( parentControl == null )
					return new Point( 0, 0 );
				else
					return AddPoints( parentControl.Position, parentControl.Offset );
				//return offset; 
			}
			//set { offset = value; }
		}

		public bool Enabled
		{
			get { return enabled; }
			set { enabled = value; }
		}
		public bool Visible
		{
			get { return visible; }
			set { visible = value; }
		}

		//private Point GetOffset()
		//{
		//    if (parentControl == null)
		//        return new Point( 0, 0 );
		//    else
		//        return parentControl.Position;
		//}
		//public SdlDotNet.Input.

		public bool IsPointWithinControl( Point p )
		{
			if ( ( position.X + Offset.X ) < p.X && p.X < ( position.X + Offset.X + size.Width ) &&
				( position.Y + Offset.Y ) < p.Y && p.Y < ( position.Y + Offset.Y + size.Height )
				)
				return true;
			else
				return false;
		}

		protected Point AddPoints( Point p1, Point p2 )
		{
			int x = p1.X + p2.X;
			int y = p1.Y + p2.Y;
			return new Point( x, y );
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="screen"></param>
        /// <param name="mysurface">a surface with a border</param>
        /// <returns>the rectangle that defines the content inside the border</returns>
        protected Rectangle RenderBorder(out Surface mysurface)
        {
            SurfaceCollection col;
            col = new SurfaceCollection();
            col.Add("c:\\data\\k242\\gui\\border2.bmp", new Size(4, 4));
            //col.Add( new Surface(

            col.Transparent = true;
            col.TransparentColor = Color.Black;

            //---

            Rectangle r0 = new Rectangle(0, 0, col[0].Width, col[0].Height);															// til col[0]
            Rectangle r2 = new Rectangle(size.Width - col[0].Width, 0, col[2].Width, col[2].Height);								// til col[2]
            Rectangle r6 = new Rectangle(0, size.Height - col[4].Height, col[4].Width, col[4].Height);									// til col[4]
            Rectangle r8 = new Rectangle(size.Width - col[5].Width, size.Height - col[5].Height, col[5].Width, col[5].Height);	// til col[5]

            Rectangle r1 = new Rectangle(r0.Width, 0, size.Width - r0.Width - r2.Width, col[1].Height);	// til col[1]
            Rectangle r3 = new Rectangle(0, r0.Height, col[3].Width, size.Height - r0.Height - r6.Height);	// til col[3]
            Rectangle r5 = new Rectangle(size.Width - col[3].Width, r2.Height, col[3].Width, size.Height - r2.Height - r8.Height);	// til col[3]
            Rectangle r7 = new Rectangle(r6.Width, size.Height - col[1].Height, size.Width - r6.Width - r8.Width, col[1].Height);	// til col[1]

            Rectangle rCenter = new Rectangle(r0.Width, r0.Height, size.Width - r3.Width - r5.Width, size.Height - r1.Height - r7.Height);

            mysurface = new Surface(size);
            //surface.Fill( Color.Gray);
            //surface.Blit( new Surface( "c:\\data\\k242\\gui\\border2.gif" ), new Point( 0, 0 ) );
            mysurface.Blit(col[0], r0);
            mysurface.Blit(col[1].CreateStretchedSurface(r1.Size), r1);
            mysurface.Blit(col[2], r2);
            mysurface.Blit(col[3].CreateStretchedSurface(r3.Size), r3);
            mysurface.Blit(col[3].CreateStretchedSurface(r5.Size), r5);
            mysurface.Blit(col[4], r6);
            mysurface.Blit(col[1].CreateStretchedSurface(r7.Size), r7);
            mysurface.Blit(col[5], r8);

            return rCenter;
        }

	}

	public class ControlCollection : CollectionBase
	{
		private Control owner;
		public event SdlControlCollectionEvent OnItemAdded;
		//public event SdlControlCollectionEvent OnItemRemoved;
		public delegate void SdlControlCollectionEvent( Control control );

		public ControlCollection( Control owner )
		{
			this.owner = owner;
		}

		public void Add( Control control )
		{
			control.ParentControl = owner;
			List.Add( control );
			if ( OnItemAdded != null )
				OnItemAdded( control );
		}

		public Control this[int i]
		{
			get { return ( Control )List[i]; }
			//set { List[i] = value; }
		}
	}
}
